<?php
class koudai_orderControl extends skymvc{
	public $user;
	public $userid;
	public $koudai_order_type=1;
	public $oc_where;
	public function __construct(){
		parent::__construct();
		$this->loadModel(array("user","login","koudai_product","koudai_order","koudai_order_address","koudai_order_product","gold_log","koudai_order_log","notice","user_address","district","koudai_order_cart","koudai"));
		$this->loadConfig("table");
		
		$this->login->checkLogin();
		$this->userid=$this->login->userid;
		$this->user=$this->login->getUser();
		
		$this->oc_where=$this->login->userid?" (userid=".$this->login->userid." or oc_ssid='".OC_SSID."')  ":"oc_ssid='".OC_SSID."' ";
		$this->oc_where.=" AND siteid=".SITEID;
	}
	public function onDefault(){
		
	}
	
	public function onConfirm(){
		 
		$user=$this->user;
		$shopid=get_post('shopid','i');
		$koudai=M("koudai")->selectRow("id=".$shopid);
		if(!$koudai){
			$this->goALl("夜猫店不存在",1);
		}
		$where=$this->oc_where." AND shopid=".$shopid;
		$koudai_orderCart=M('koudai_order_cart')->select(array("where"=>$where));
		if(empty($koudai_orderCart)) $this->goall("请先去选购产品",1,0,"/index.php");
		//收货地址
		foreach($koudai_orderCart as $v){				
			$object_ids[]=$v['object_id'];
			$productids[]=$v['productid'];
			$ksids[]=$v['ksid'];
		}
		if($koudai_orderCart){
				$products=M('product')->id_list(array("where"=>" id in("._implode($productids).") "));
				$kslist=M('product_ks')->id_list(array("where"=>"  id in("._implode($ksids).") "));
		}
		
		$total_money=0; 
		$total_num=0;
			foreach($koudai_orderCart as $k=>$v){
			 	
				$product=$products[$v['productid']]; 
				$v['title']=$product['title'];
				$v['imgurl']=images_site($product['imgurl']);
				if(isset($kslist[$v['ksid']])){
					$ks=$kslist[$v['ksid']];
					$v['price']=$product['lower_price']>0?$product['lower_price']:$ks['price'];
					$v['ks_title']=$ks['title'];
				}else{
					$v['price']=$product['lower_price']>0?$product['lower_price']:$product['price'];
				}
				$total_money+=$v['price']*$v['amount'];
				$total_num+=$v['amount'];
				$koudai_orderCart[$k]=$v;
			}
		$youka=M("youka")->get();
		
		$address=$this->user_address->select(array("where"=>"userid=".$user['userid']." AND siteid=".SITEID,"shop_order"=>"isdefault desc","limit"=>1));
		if($address){
			foreach($address as $v){
				$d_ids[]=$v['province_id'];
				$d_ids[]=$v['city_id'];
				$d_ids[]=$v['town_id'];
			}
			$dist_list=M("site_city")->id_title(array("where"=>" sc_id in(".implode(",",$d_ids).") ","start"=>0,"limit"=>1000000)); 
		}
		if($total_money<$koudai['qsjia']){
			$this->goALl("商品价格小于起送价".$koudai['qsjia']."元，不能下单哦");
		}
		$this->smarty->assign(array(
			"address"=>$address,
			"dist_list"=>$dist_list,
			"back_url"=>$_SERVER['HTTP_REFERER'],
			"koudai_orderCart"=>$koudai_orderCart,
			"koudai"=>$koudai,
			"total_money"=>$total_money,
			"user"=>$user,
			"total_num"=>$total_num,
			"youka"=>$youka,
			"ssyouka"=>M("youka")->get($this->login->userid,2)
		));
		$this->smarty->display("koudai_order/confirm.html");
		
	}
	

	
	public function onorder(){
		M("koudai_order")->begin();
		$user=$this->user;
		$shopid=post('shopid','i');
		$user_address_id=post('user_address_id','i');
		$addr=$this->user_address->selectRow(array("where"=>"id=".$user_address_id));
		if(empty($addr) or empty($addr['telephone'])){
			$this->goall("请完善收货地址",1);
		}
		//处理商品
		$ids=post('cart_id','i');
		if($ids){
			$cart_list=$this->koudai_order_cart->select(array("where"=> $this->oc_where ." AND id in("._implode($ids).") "));
			 
			if(empty($cart_list)) $this->goall("请选择要购买的商品",1);
			$total_money=0;
			$koudais=array();
			foreach($cart_list as $v){				
				$object_ids[]=$v['object_id'];
				$ksids[]=$v['ksid'];
				$productids[]=$v['productid'];
			}
			
			if($cart_list){
				$products=M('product')->id_list(array("where"=>" id in("._implode($productids).") "));
				$kslist=M('product_ks')->id_list(array("where"=>"  id in("._implode($ksids).") "));
				$ymproducts=M('koudai_product')->id_list(array("where"=>" id in("._implode($object_ids).") "));
			}
			foreach($cart_list as $k=>$v){
				$product=$products[$v['productid']];
				$ymproduct=$ymproducts[$v['object_id']];
				if(empty($product) or ($product['status']!=2)){
					$this->goall("产品".$t_d['title']."已下线，请删除该商品",1);
				}
				if($ymproduct['total_num']<$v['amount']){
					$this->goall("产品<a href='".R("/index.php?m=show&id=".$product['id'])."'>".$product['title']."</a>库存不足，当前还剩".($ymproduct['total_num'])."件，请删除该商品",1);
				}
				$v['price']=$product['lower_price']>0?$product['lower_price']:$product['price'];
				$total_money +=$v['price']*$v['amount'];
				if($v['lower_price']>0){
					$discount_money+=($product['price']-$product['lower_price'])*$v['amount'];
				}
				$cart_list[$k]=$v;
			}
			
			
			
		}else{
			$this->goall("请选择要购买的商品",1);
		}
		
		//End 处理商品
		$koudai=M("koudai")->selectRow("id=".$shopid);
		if($total_money<$koudai['qsjia']){
			$this->goALl("商品价格小于起送价".$koudai['qsjia']."元，不能下单哦");
		}
		$isfinish=0;
		$ispay=1;
		$this->loadControl("jfapi");
		$goods_money=$total_money;
		$express_money=EXPRESS_FEE;
		$total_money=$total_money+$express_money;
		$fenqi=post('fenqi','i');
		$youka=M("youka")->get($this->login->userid,$fenqi);
		
		if($fenqi && ($youka['money']<$total_money)){
			$this->goAll("您的授信额度不足，请选择其他付款方式",22);
		}
		/* Start优惠券 */
		$coupon_id=post('coupon_id','i');
		if($coupon_id){
			$this->loadModel(array("coupon","coupon_user"));
			$coupon=$this->coupon->selectRow("id=".$coupon_id." AND siteid=".SITEID);
			if(!in_array($coupon['type_id'],array(1,3)) or $goods_money<$coupon['lower_money']){
				$this->goAll("当前优惠券不能使用",1,$coupon);
			}
			$coupon_user=$this->coupon_user->selectRow("coupon_id=".$coupon_id." AND status=0 AND userid=".$this->login->userid);			
			if($coupon && $coupon_user){
				$coupon_money=$coupon['money'];
				$this->coupon_user->update(array("status"=>1),"id=".$coupon_user['id']);
				$this->coupon->update(array("use_num"=>$coupon['use_num']+1),"id=".$coupon_id);
				$total_money=$total_money-$coupon_money;
			}else{
				$coupon_id=0;
			}						
		}else{
			$coupon_money=0;
		}
		/* End 优惠券*/
		if($fenqi){
			/*处理分期付款*/
			M("youka")->addMoney(array(
					"userid"=>$this->login->userid,
					"typeid"=>$fenqi,
					"money"=>-$total_money,
					"content"=>"您购买了产品花了[money]元信用额度",			
			));
				 
			 $ispay=2;
			 $isfinish=1;
		}else{
			//处理余额消费
			$pay_type=get_post('pay_type','h');
			if($paytype=='money' &&  $total_money<=$user['money']){
				//添加金钱消费记录		
				M("user")->addMoney(array(
						"userid"=>$this->login->userid,
						"money"=>-$total_money,
						"content"=>"您购买了产品花了[money]元",	
				));
				$ispay=2;
				$isfinish=1;		
			}
			
			if(post('unpay','i')){
				$isfinish=1;
				$unpay=post('unpay','i');
			}
		}
			//处理订单
			$last_ykmoney=0;
			if($fenqi){
				$lastfq=M("koudai_order")->getGzProduct($user['userid']);
				$oids=$lastfq['oids']; 
				$gzproducts=$lastfq['products'];
				$last_ykmoney=$lastfq['money'];
				if($lastfq){
					M("koudai_order")->update(array(
						"isadd"=>1
					),"order_id in("._implode($oids).")");
				}
			}
			$order_id=$this->koudai_order->insert(array(
				"orderno"=>"koudai_".date("YmdHis").$user['userid'],
				"dateline"=>time(),
				"ispay"=>$ispay,
				"userid"=>$user['userid'],
				"send_id"=>post('send_id','i'),
				"comment"=>post('comment','h'),
				"money"=>$total_money,
				"express_money"=>$express_money,
				"goods_money"=>$goods_money,
				"discount_money"=>$$discount_money,
				"user_address_id"=>$user_address_id,
				"object_id"=>0,
				"unpay"=>$unpay,
				"shopid"=>$shopid,
				"coupon_id"=>$coupon_id,
				"coupon_money"=>$coupon_money,
				"sushe"=>$addr['sushe'],
				"sc_id"=>$addr['city_id'],
				"fenqi"=>$fenqi,
				"susheid"=>intval($youka['sushe']),
				"last_ykmoney"=>$last_ykmoney,
				"youka"=>intval($youka['id']),
				"siteid"=>SITEID
			));
			//处理宿舍成员关联
			if($fenqi==2){
				$sscy=M("sushe")->getcyByUser($this->login->userid);
				if($sscy){
					foreach($sscy as $v){
						M("koudai_order_ssuser")->insert(array(
							"order_id"=>$order_id,
							"userid"=>$v['userid']
						));
					}
				}
			}
			
			//处理上期压款订单
			
			//处理订单的产品
			if($order_id){
				
				foreach($cart_list as $k=>$v){
					if(isset($gzproducts[$v['productid']])){
						$amount=$v['amount']+$gzproducts[$v['productid']]['gznum'];
						unset($gzproducts[$v['productid']]);
					}else{
						$amount=$v['amount'];
						
					}
					$this->koudai_order_product->insert(array(
						"order_id"=>$order_id,
						"userid"=>$user['userid'],
						"object_id"=>$v['object_id'],
						"productid"=>$v['productid'],
						"type_id"=>$v['type_id'],
						"price"=>$v['price'],
						"amount"=>$amount,
						"dateline"=>time(),
						"ksid"=>$v['ksid'],
						 
					));
					//减掉商品库存
					M("koudai_product")->total_num_add(array(
						"shopid"=>$v['shopid'],
						"productid"=>$v['productid'],
						"total_num"=>-$v['amount'],
						"content"=>"您下了单产品[title]库存减少[total_num]"
					));
					
				}
				if(!empty($gzproducts)){
					foreach($gzproducts as $v){
						$this->koudai_order_product->insert(array(
							"order_id"=>$order_id,
							"userid"=>$user['userid'],
							"object_id"=>$v['object_id'],
							"productid"=>$v['productid'],
							"type_id"=>$v['type_id'],
							"price"=>$v['price'],
							"amount"=>$v['gznum'],
							"dateline"=>time(),

							 
						));
					}
				}
				
			}else{
				$this->goall("订单处理失败",1,0,$back_url);
			}
			
			//处理订单收货地址
			$d_ids=array($addr['province_id'],$addr['city_id'],$addr['town_id']);
			$dist_list=M("site_city")->id_title(array("where"=>" sc_id in(".implode(",",$d_ids).") ","start"=>0,"limit"=>1000000)); 
			$this->koudai_order_address->insert(array(
				"order_id"=>$order_id,
				"userid"=>$user['userid'],
				"truename"=>$addr['truename'],
				"telephone"=>$addr['telephone'],
				"p_c_t"=>$dist_list[$addr['province_id']].$dist_list[$addr['city_id']].$dist_list[$addr['town_id']],
				"address"=>$addr['address'],
				"dateline"=>time(),
				"sushe"=>$addr['sushe']
			));
		 
		//清除购物车
		
		$this->koudai_order_cart->delete($this->oc_where." AND id in("._implode($ids).")");
		//发送推送信息
		$content=date("H:i:s")."您的口袋店有新的订单了，赶紧去处理下吧";
		M("apppush")->sendShop(array(
				"table"=>"koudai",
				"content"=>$content,
				"shopid"=>$shopid,
				"retable"=>"koudai"	
			));
		//End 推送信息
		M("koudai_order")->commit();
		if($isfinish){
			$this->goall($this->lang['koudai_order_success'],0,0,"/index.php?m=koudai_order&a=show&order_id=".$order_id);
		}else{
			//header("Location: /index.php?m=recharge&a=default&table=koudai&pay_type=".$pay_type."&order_id=".$order_id);
			$this->goall("下单成功",0,0,"/index.php?m=koudai_order&a=pay&pay_type=".$pay_type."&order_id=".$order_id);
		}
	}
	
	public function onPay(){
		$order_id=get('order_id','i');
		$pay_type=get('pay_type','h');
		$this->smarty->assign(array(
			"payurl"=>"/index.php?m=recharge&a=default&table=koudai&pay_type=".$pay_type."&order_id=".$order_id,
			"order"=>M("koudai_order")->selectRow("order_id=".$order_id)
		));
		$this->smarty->display("koudai_order/pay.html");
	}
	/*分期付款*/
	public function onMyIndex(){
		$where="  siteid=".SITEID." AND status<8 AND  fenqi=1 AND isdz=0 AND userid=".$this->userid;
		$url=APPINDEX."?m=koudai_order&a=myindex";
		$start=get('per_page','i');
		$limit=3;
		$option=array(
			"where"=>$where,
			"order"=>"order_id DESC",
			"start"=>$start,
			"limit"=>$limit
		);
		$rscount=true;
		$data=$this->koudai_order->select($option,$rscount);
		if($data){
			foreach($data as $k=>$v){
				$v['address']=$this->koudai_order_address->selectRow(array("where"=>"order_id=".$v['order_id'],"koudai_order"=>"id DESC"));
			 
				$v['koudai_product']=$this->orderproduct($v['order_id']);
				 
				$data[$k]=$v;
			}
		}
		
		/***宿舍订单**/
		$sushe=M("sushe_user")->selectRow("userid=".$this->login->userid);
	 
		if($sushe){
			$option=array(
				"where"=>"susheid=".$sushe['sushe']." AND isdz<2  ",
				"order"=>"order_id DESC",
				"start"=>0,
				"limit"=>10
			);
			$ssdata=$this->koudai_order->select($option);
			if($ssdata){
			foreach($ssdata as $k=>$v){
					$v['address']=$this->koudai_order_address->selectRow(array("where"=>"order_id=".$v['order_id'],"koudai_order"=>"id DESC"));
				 
					$v['koudai_product']=$this->orderproduct($v['order_id']);
					 
					$ssdata[$k]=$v;
				}
			}
		}
		$pagelist=$this->pagelist($rscount,$limit,$url);
		$order_status_list=$this->config_item('order_status_list');
		$order_type_list=$this->config_item('order_type_list');
		$order_ispay=$this->config_item('order_ispay');
		$this->smarty->assign(array(
			"data"=>$data,
			"ssdata"=>$ssdata,
			"order_product"=>$koudai_order_product,
			"order_status_list"=>$order_status_list,
			"order_type_list"=>$order_type_list,
			"order_ispay"=>$order_ispay,
		));
		$this->smarty->display("koudai_order/myindex.html");
	}
	
	public function onDuiZhang(){
		
		$order_id=get('order_id','i');
		$data=$this->koudai_order->selectRow(array("where"=>"order_id=".$order_id));
		if(empty($data)) $this->goall("参数出错",1);
		if($data['userid']!=$this->login->userid){
			$this->goall("该订单不是您的",1);
		}
		if($data['status']!=3){
			$this->goall("该订单还未配送完成，不能对账哦",1);
		}
		$data['products']=$this->orderproduct($order_id);
		$gz_money=$th_money=$js_money=0;
		$gz_num=$th_num=$js_num=0;
		if($data['products']){
			foreach($data['products'] as $v){
				$gz_money +=$v['gznum']*$v['order_price'];
				$th_money +=$v['thnum']*$v['order_price'];
				$js_money +=$v['jsnum']*$v['order_price'];
				$gz_num+=$v['gznum'];
				$th_num+=$v['thnum'];
				$js_num+=$v['jsnum'];
			}
		}
		$this->smarty->assign(array(
			"data"=>$data,
			"gz_money"=>$gz_money,
			"th_money"=>$th_money,
			"js_money"=>$js_money,
			"gz_num"=>$gz_num,
			"th_num"=>$th_num,
			"js_num"=>$js_num,
		));
		switch(get('op')){
			case 'tuihuo':
				$tpl="duizhang_tuihuo.html";
				break;
			case 'jiesuan':
				$tpl="duizhang_jiesuan.html";
				break;
			default:
				$tpl="duizhang.html";
				break;
		}
		if($data['isdz']){
			$this->smarty->display("koudai_order/duizhanged.html");
		}else{
			$this->smarty->display("koudai_order/".$tpl);
		}
	}
	
	public function onDuiZhangNum(){
		$order_id=get('order_id','i');
		$data=$this->koudai_order->selectRow(array("where"=>"order_id=".$order_id));
		if(empty($data)) $this->goall("参数出错",1);
		if($data['fenqi']==2){
			$sscy=M("sushe_user")->selectCols(array(
				"where"=>"sushe=".$data['susheid'],
				"fields"=>"userid"
			));
			if(!in_array($data['userid'],$sscy) ){
				$this->goall("该订单不是您的",1);
			}

		}else{
			if($data['userid']!=$this->login->userid ){
				$this->goall("该订单不是您的",1);
			}
		}
		if(!$data['fenqi']){
			$this>goAll("该订单不是分期订单，无法对账");
		}
		
		if($data['status']!=3){
			$this->goall("该订单还未完成，不能对账哦",1);
		}
		
		if($data['isdz']){
			$this->goall("该订单已经对账了",1);
		}
		$id=get('id','i');
		$product=M("koudai_order_product")->selectRow("id=".$id);
		$pdata['gznum']=isset($_GET['gznum'])?get('gznum','i'):$product['gznum'];
		$pdata['thnum']=isset($_GET['thnum'])?get('thnum','i'):$product['thnum'];
		if(($pdata['gznum']+$pdata['thnum'])>$product['amount']){
			$this->goAll("保存失败，对账数量超出",1);
		}
		$pdata['jsnum']=$product['amount']-$pdata['gznum']-$pdata['thnum'];
		if(($pdata['gznum']+$pdata['thnum']+$pdata['jsnum'])>$product['amount']){
			$this->goAll("保存失败，对账数量超出",1);
		}
		M("koudai_order_product")->update($pdata,"id=".$id);
		$products=$this->orderproduct($order_id);
		$gz_money=$th_money=$js_money=0;
		$gz_num=$th_num=$js_num=0;
		if($products){
			foreach($products as $v){
				$gz_money +=$v['gznum']*$v['order_price'];
				$th_money +=$v['thnum']*$v['order_price'];
				$js_money +=$v['jsnum']*$v['order_price'];
				$gz_num+=$v['gznum'];
				$th_num+=$v['thnum'];
				$js_num+=$v['jsnum'];
			}
		}
		$data=array(
			"gz_money"=>$gz_money,
			"th_money"=>$th_money,
			"js_money"=>$js_money,
			"gz_num"=>$gz_num,
			"th_num"=>$th_num,
			"js_num"=>$js_num,
		);
		$this->goAll("保存成功",0,$data);
	}
	
	public function onDuiZHangPay(){
		$order_id=get('order_id','i');
		$data=$this->koudai_order->selectRow(array("where"=>"order_id=".$order_id));
		if(empty($data)) $this->goall("参数出错",1);
		if($data['fenqi']==2){
			$sscy=M("sushe_user")->selectCols(array(
				"where"=>"sushe=".$data['susheid'],
				"fields"=>"userid"
			));
			if(!in_array($data['userid'],$sscy) ){
				$this->goall("该订单不是您的",1);
			}

		}else{
			if($data['userid']!=$this->login->userid ){
				$this->goall("该订单不是您的",1);
			}
		}
		if($data['status']!=3){
			$this->goall("该订单还未配送完成，不能对账哦",1);
		}
		
		$gz_money=$th_money=$js_money=0;
		$gz_num=$th_num=$js_num=0;
		$order_product=M("koudai_order_product")->select(array(
			"where"=>"order_id=".$order_id
		));
		if($order_product){
			foreach($order_product as $v){
				M("koudai_order_product")->update(array(
					"jsnum"=>$v['amount']-$v['gznum']-$v['thnum']
				),"id=".$v['id']);
			}
		}
		if($order_product){
			foreach($order_product as $k=>$v){
				$productids[]=$v['productid'];
			}
			$products=M("product")->getListByIds($productids);
			foreach($order_product as $k=>$v){
				$p=$products[$v['productid']];
				$p['order_price']=$v['price'];
				$p['amount']=$v['amount'];
				$p['total_money']=$v['amount']*$v['price'];
				$p['gznum']=$v['gznum'];
				$p['thnum']=$v['thnum'];
				$p['jsnum']=$p['amount']-$v['gznum']-$v['thnum'];
				$p['order_product_id']=$v['id'];
				$order_product[$k]=$p;
			}
		}
		$data['products']=$order_product;
		if($data['products']){
			//处理结算
			
			foreach($data['products'] as $v){
				$gz_money +=$v['gznum']*$v['order_price'];
				$th_money +=$v['thnum']*$v['order_price'];
				$js_money +=$v['jsnum']*$v['order_price'];
				$gz_num+=$v['gznum'];
				$th_num+=$v['thnum'];
				$js_num+=$v['jsnum'];
			}
		}
		$this->smarty->assign(array(
			"data"=>$data,
			"gz_money"=>$gz_money,
			"th_money"=>$th_money,
			"js_money"=>$js_money,
			"gz_num"=>$gz_num,
			"th_num"=>$th_num,
			"js_num"=>$js_num,
		));
		switch(get('op')){
			case 'tuihuo':
				$tpl="duizhang_tuihuo.html";
				break;
			case 'jiesuan':
				$tpl="duizhang_jiesuan.html";
				break;
			default:
				$tpl="duizhang.html";
				break;
		}
		$this->smarty->display("koudai_order/duizhangpay.html");
	}
	
	public function onDuiZhangSave(){
		$order_id=get('order_id','i');
		$data=$this->koudai_order->selectRow(array("where"=>"order_id=".$order_id));
		
		if(empty($data)) $this->goall("参数出错",1);
		if($data['fenqi']==2){
			$sscy=M("sushe_user")->selectCols(array(
				"where"=>"sushe=".$data['susheid'],
				"fields"=>"userid"
			));
			if(!in_array($data['userid'],$sscy) ){
				$this->goall("该订单不是您的",1);
			}

		}else{
			if($data['userid']!=$this->login->userid ){
				$this->goall("该订单不是您的",1);
			}
		}
		if(!$data['fenqi']){
			$this>goAll("该订单不是分期订单，无法对账");
		}
		if($data['status']!=3){
			$this->goall("该订单还未完成，不能对账哦",1);
		}
		$pay_type=get_post('paytype','h');
		if($data['isdz']){
			if($data['dzpay']==1){
				header("Location: /index.php?m=koudai_order&a=dzpay&order_id=".$order_id."&pay_type=".$pay_type);
				exit();
			}
			$this->goall("该订单已经对账了",1,0,"/index.php?m=koudai_order&a=duizhang&order_id=".$order_id);
		}
		
		$kdproducts=M("koudai_order_product")->select(array(
			"where"=>"order_id=".$order_id
		));
		 
		$dzmoney=0;
		foreach($kdproducts as $v){
			$dzmoney+=$v['price']*$v['jsnum'];
		}
		 
		 if($dzmoney>0){
			 //测试 为 2 正常 1
			 $dzpay=2;
		 }else{
			 $dzpay=2;
		 }
		$this->koudai_order->update(array(
			"isdz"=>1,
			"dzmoney"=>$dzmoney,
			"dzpay"=>$dzpay
		),"order_id=".$order_id);
		//确认
		if($data['fenqi']==2){
			M("koudai_order_ssuser")->update(array(
				"status"=>1
			),"order_id=".$order_id);
		}
		if($dzpay==1){
			header("Location: /index.php?m=koudai_order&a=dzpay&order_id=".$order_id."&pay_type=".$pay_type);
		}else{
			$this->goALl("对账完成",0,0,"/index.php?m=koudai_order&a=duizhang&order_id=".$order_id);
		}
		
	}
	
	public function ondzPay(){
		$order_id=get('order_id','i');
		$pay_type=get('pay_type','h');
		$this->smarty->assign(array(
			"payurl"=>"/index.php?m=recharge&a=koudai_dz&table=koudai&pay_type=".$pay_type."&order_id=".$order_id,
			"order"=>M("koudai_order")->selectRow("order_id=".$order_id)
		));
		$this->smarty->display("koudai_order/dzpay.html");
	}
	
	public function ondzLog(){
		$where="   userid=".$this->userid;
		$url=APPINDEX."?m=koudai_order&a=dzlog";
		$start=get('per_page','i');
		$limit=20;
		$option=array(
			"where"=>$where,
			"order"=>"id DESC",
			"start"=>$start,
			"limit"=>$limit
		);
		$rscount=true;
		$data=M("koudai_dz")->select($option,$rscount);
		if($data){
			foreach($data as $k=>$v){
				$ps=M("koudai_order_product")->select(array(
					"order_id"=>$v['order_id']
				));
				$gznum=$thnum=$jsnum=0;
				$gzmoney=$thmoney=$jsmoney=0;
				foreach($ps as $p){
					$gznum+=$p['gznum'];
					$gzmoney+=$p['gznum']*$p['price'];
					$thnum+=$p['thnum'];
					$thmoney+=$p['thnum']*$p['price'];
					$jsnum+=$p['jsnum'];
					$jsmoney+=$p['jsnum']*$p['price'];	
				}
				$v['gznum']=$gznum;
				$v['gzmoney']=$gzmoney;
				$v['thnum']=$thnum;
				$v['thmoney']=$thmoney;
				$v['jsnum']=$jsnum;
				$v['jsmoney']=$jsmoney;
				$v['admin']=M("koudai_admin")->selectRow("adminid=".$v['adminid']);
				$data[$k]=$v;
			}
			
		}
	 
		$this->smarty->assign(array(
			"data"=>$data	
		));
		$this->smarty->display("koudai_order/dzlog.html");
	}
	
	public function onMy(){
		$where=" siteid=".SITEID." AND   status<8 AND userid=".$this->userid;
		$url=APPINDEX."?m=koudai_order&a=my";
		$start=get('per_page','i');
		$limit=20;
		$option=array(
			"where"=>$where,
			"order"=>"order_id DESC",
			"start"=>$start,
			"limit"=>$limit
		);
		$rscount=true;
		$data=$this->koudai_order->select($option,$rscount);
		if($data){
			foreach($data as $k=>$v){
				$v['address']=$this->koudai_order_address->selectRow(array("where"=>"order_id=".$v['order_id'],"koudai_order"=>"id DESC"));
			 
				$v['koudai_product']=$this->orderproduct($v['order_id']);
				 
				$data[$k]=$v;
			}
		}
		$pagelist=$this->pagelist($rscount,$limit,$url);
		$order_status_list=$this->config_item('order_status_list');
		$order_type_list=$this->config_item('order_type_list');
		$order_ispay=$this->config_item('order_ispay');
		$this->smarty->assign(array(
			"data"=>$data,
			"rscount"=>$rscount,
			"pagelist"=>$pagelist,
			"order_status_list"=>$order_status_list,
			"order_type_list"=>$order_type_list,
			"order_ispay"=>$order_ispay,
			
		));
		$this->smarty->display("koudai_order/my.html");
	}
	

	
	public function onshow(){
		 
		$order_id=get('order_id','i');
		$data=$this->koudai_order->selectRow(array("where"=>"order_id=".$order_id));
		if(empty($data)) $this->goall("参数出错",1);
		if($data['fenqi']==2){
			$sscy=M("sushe_user")->selectCols(array(
				"where"=>"sushe=".$data['susheid'],
				"fields"=>"userid"
			));
			if(!in_array($data['userid'],$sscy) ){
				$this->goall("该订单不是您的",1);
			}

		}else{
			if($data['userid']!=$this->login->userid ){
				$this->goall("该订单不是您的",1);
			}
		}
		$addr=$this->koudai_order_address->selectRow(array("where"=>"order_id=".$order_id,"koudai_order"=>"id DESC"));
		 
		$koudai_order_status_list=$this->config_item('order_status_list');
		
		$koudai_order_type_list=$this->config_item('order_type_list');
		$koudai_order_ispay=$this->config_item('order_ispay');
		//获取商品
		 
		$koudai_order_product=M('koudai_order_product')->select(array("where"=>"order_id=".$data['order_id']));
		 
		foreach($koudai_order_product as $k=>$v){
			$p=M('product')->selectRow(array("where"=>"id=".$v['productid']));
			$p['order_price']=$v['price'];
			$p['iscomment']=$v['iscomment'];
			$p['rating_grade']=$v['rating_grade'];
			$p['amount']=$v['amount'];
			
			$ks=M('product_ks')->selectRow("id=".$v['ksid']);
			if($ks){
				$p['ks_title']=$ks['title'];
				$p['price']=$ks['price'];
			}
			$koudai_order_product[$k]=$p;
		}
		 
		$this->smarty->assign(array(
			"data"=>$data,
			"addr"=>$addr,
			"order_status_list"=>$koudai_order_status_list,
			"order_type_list"=>$koudai_order_type_list,
			"order_ispay"=>$koudai_order_ispay,
			"order_product"=>$koudai_order_product,
		));
		$this->smarty->display("koudai_order/show.html");
	}
	
	public function orderproduct($order_id){
		
		$order_product=M("koudai_order_product")->select(array("where"=>"order_id=".intval($order_id)));
		if($order_product){
			foreach($order_product as $k=>$v){
				$productids[]=$v['productid'];
			}
			$products=M("product")->getListByIds($productids);
			foreach($order_product as $k=>$v){
				$p=$products[$v['productid']];
				$p['order_price']=$v['price'];
				$p['amount']=$v['amount'];
				$p['total_money']=$v['amount']*$v['price'];
				$p['gznum']=$v['gznum'];
				$p['thnum']=$v['thnum'];
				$p['jsnum']=$p['amount']-$v['gznum']-$v['thnum'];
				$p['order_product_id']=$v['id'];
				$order_product[$k]=$p;
			}
		}
		return $order_product;	
	}
	
	public function onReceive(){
		$order_id=get('order_id','i');
		$data=$this->koudai_order->selectRow(array("where"=>" order_id=".$order_id." AND userid=".$this->userid." "));
		if(empty($data) or $data['status']<2){
			$this->goall($this->lang['data_no_exists'],1,0,"/index.php");
		}
		$this->koudai_order->update(array("isreceived"=>2)," order_id=".$order_id." ");
		$this->goall($this->lang['save_success'],0,$data);
	}
	
	public function oncomment(){
		$order_id=get_post('order_id','i');
		$id=get_post('id','id');
		$order=$this->koudai_order->selectRow("order_id=".$order_id);
		if($order['userid']!=$this->login->userid or $order['status']!=3 ){
			$this->goall ("订单信息出错",1);
		}
		$shop=M("koudai")->selectRow("id=".$order['shopid']);
		$comment=M("koudai_order_comment")->selectRow("order_id=".$order_id);
		$this->smarty->assign(array(
			"order"=>$order,
			"shop"=>$shop,
			"comment"=>$comment
		)); 
		$this->smarty->display("koudai_order/comment.html");
	}
	
	
	
	public function onCommentSave(){
		$order_id=get_post('order_id','i');
		$id=get_post('id','id');
		$order=$this->koudai_order->selectRow("order_id=".$order_id);
		if($order['userid']!=$this->login->userid or $order['status']!=3 ){
			$this->goall ("订单信息出错",1);
		}
		$comment=M("koudai_order_comment")->selectRow("order_id=".$order_id);
		$data=M("koudai_order_comment")->postData();
		$data['siteid']=SITEID;
		$data['shopid']=$order['shopid'];
		$data['dateline']=time();
		$data['userid']=$this->login->userid;
		if($comment){
			M("koudai_order_comment")->update($data,"id=".$comment['id']);
		}else{
			M("koudai_order_comment")->insert($data);
		}
		$this->goAll("评价成功",1);		
	}
	
	public function onproduct_comment(){
		$order_id=get_post('order_id','i');
		$id=get_post('id','id');
		$koudai_order=$this->koudai_order->selectRow("order_id=".$order_id);
		if($koudai_order['userid']!=$this->userid or $koudai_order['status']!=3 ){
			$this->goall ("订单信息出错",1);
		}
		$row=$this->koudai_order_product->selectRow(" order_id=".$order_id." AND object_id=".$id." ");
		if(empty($row) or $row['iscomment'] ){
			$this->goall("产品出错",1);
		}
		$this->smarty->display("koudai_order/koudai_product_comment.html");
	}
	
	public function onproduct_comment_save(){
		$order_id=get_post('order_id','i');
		$id=get_post('id','id');
		$koudai_order=$this->koudai_order->selectRow("order_id=".$order_id);
		if($koudai_order['userid']!=$this->userid or $koudai_order['status']!=3 ){
			$this->goall("订单信息出错",1);
		}
		$row=$this->koudai_order_product->selectRow(" order_id=".$order_id." AND object_id=".$id." ");

		if(empty($row) or $row['iscomment']){
			$this->goall("产品出错",1);
		}
		$this->loadModel(array("rating"));
		$data=array(
			"grade"=>post('grade','i'),
			"userid"=>$this->userid,
			"object_id"=>$id,
			"type_id"=>1,
			"dateline"=>time(),
			"status"=>0,
			"model_id"=>MODEL_koudai_PRODUCT_ID,
			"content"=>post('content','h'),
			"order_id"=>$order_id
		);
		$this->rating->insert($data);
		$this->koudai_order_product->update(array("iscomment"=>1,"rating_grade"=>post('grade','i'))," order_id=".$order_id." AND object_id=".$id." ");
		$pro=$this->koudai_product->selectRow("id=".$id);
		$rating_grade=$this->rating->selectOne(array("where"=>" model_id=".MODEL_koudai_product_ID." AND type_id=1 AND object_id=".$id." ","fields"=>" AVG(grade) as g"));
		$this->koudai_product->update(array("rating_grade"=>$rating_grade,"rating_num"=>$pro['rating_num']+1)," id=".$id);
		$this->goall("评论成功");
	}
	
	public function onCancel(){
		M("koudai_order")->begin();
		$order_id=get_post('order_id','i');
		$data=$this->koudai_order->selectRow(array("where"=>"order_id=".$order_id));
		if(empty($data)) $this->goall("参数出错",1);
		if($data['status']>=3) $this->goall("操作失败",1);
		$this->koudai_order->update(array("status"=>10),"order_id=".$order_id);
		$content=post('content');
		$message=post('message','h');
		$this->koudai_order_log->insert(array(
			"dateline"=>time(),
			"admin_id"=>$this->admin['id'],
			"order_id"=>$order_id,
			"content"=>$content."，原因：".$message,
		));
		
		//加商品库存
		$order_product=M("koudai_order_product")->select(array("where"=>"order_id=".$order_id));
		foreach($order_product as $v){
			M("koudai_product")->total_num_add(array(
				"shopid"=>$v['shopid'],
				"productid"=>$v['productid'],
				"total_num"=>$v['amount'],
				"content"=>"[下单]"
			));
		}
	 
		//退钱给用户
		if($data['fenqi']){
			/*处理分期付款*/
			M("youka")->addMoney(array(
					"userid"=>$this->login->userid,
					"money"=>$data['money'],
					"typeid"=>$order['fenqi'],
					"youka"=>$order['youka'],
					"content"=>"您的订单取消了，信用余额加了[money]元",			
			));
		}elseif($data['ispay']==2){
			$this->loadControl("jfapi","source/index/");
			$user=$this->user->selectRow(array("where"=>"userid=".$data['userid']));
			$this->jfapiControl->setUserId($data['userid'])->addMoney(array(
						"money"=>$data['money'],
						"type_id"=>1,
						"ispay"=>2,
						"content"=>"您的订单被取消了，退还给您￥".$data['lower_price']."，之前有￥[oldmoney]，目前还剩￥[newmoney]",
			));
		}
		M("koudai_order")->commit();
		$this->goall("取消成功");
	}
	
}
?>